%% 2Choice Task
fig = findobj('type','figure','-and','name','HiLo');
if isempty(fig)
    fig = figure('name','HiLo');
end
figure(fig)

rv = schedule.response_vals;

rcidx = regexp(schedule.readparams,'response_code');
rcidx = findincell(rcidx) + 1;
ttidx = regexp(schedule.readparams,'trial_type');
ttidx = findincell(ttidx) + 1;
hpidx = findincell(regexp(schedule.readparams,'HPFreq'));

vals   = rv(:,rcidx); vals(isnan(vals))     = [];
trials = rv(:,ttidx); trials(isnan(trials)) = [];
freqs  = rv(:,hpidx);  freqs(isnan(freqs))   = [];


% --- exclude first N trials ---
N = 0;
if N == 0 && length(vals) > 100
    N = length(vals)-100;
    vals(1:N)   = [];
    trials(1:N) = [];
elseif N > 0
    vals(1:N)   = [];
    trials(1:N) = [];
end
festr = sprintf('\t*%d trials excluded',N);
%----------------------------------------------------

hits = bitget(vals,6);

% Probe Higher State = 2
PHright = bitget(vals,4) & bitget(vals,13);
PHleft  = bitget(vals,3) & bitget(vals,13);

% Probe Lower State = 1
PLright = bitget(vals,4) & bitget(vals,12);
PLleft  = bitget(vals,3) & bitget(vals,12);

% Quiet Reference State = 0
RQright = bitget(vals,4) & bitget(vals,11);
RQleft  = bitget(vals,3) & bitget(vals,11);

PHind = PHright|PHleft;
PLind = PLright|PLleft;
RQind = RQleft|RQright;

% totals
r = sum([PHright PHleft PLright PLleft RQleft RQright]);
PHhr = r(1)/sum(r([1 2]))*100;
PLhr = r(4)/sum(r([3 4]))*100;
RQlb = r(5)/sum(r([5 6]))*100;
Ohr  = sum(PHright|PLleft)/sum(PHind|PLind)*100;

% print results to command window
fprintf('\n%s\n',schedule.schedule);
fprintf('\t\t\t\t\tRIGHT\t\tLEFT\tPerf\n')
fprintf('Probe Higher\t\t%d\t\t%d\t\t%3.1f%%\n',      r([1 2]),PHhr)
fprintf('Probe Lower\t\t\t%d\t\t%d\t\t%3.1f%%\n',       r([3 4]),PLhr)
fprintf('Quiet (lft bias)\t%d\t\t%d\t\t%3.1f%%\n',  r([5 6]),RQlb)
fprintf('#trials = %3.0f\t\t%3.1f%%%s\n\n',sum(r),Ohr,festr);


% plot by trials
% figure('windowstyle','docked','name',sprintf('Schedule: %s',schedule.schedule));
% set(gcf,'name',sprintf('Schedule: %s',schedule.schedule));
clf
subplot(10,1,1)
stairs(hits|PLright);
set(gca,'xtick',[],'ytick',[0 1],'yticklabel',{'miss' 'hit'}, ...
    'xlim',[0 length(hits)],'ylim',[-0.5 1.5]);

subplot(10,1,2:10)
hold on
grid on


PHy = cumsum(PHright)./cumsum(PHind) * 100;
PLy = cumsum(PLleft) ./cumsum(PLind) * 100;
RQy = cumsum(RQleft) ./cumsum(RQind) * 100;

plot(find(PHind&PHright),PHy(PHind&PHright),'.b');
plot(find(PLind&PLleft), PLy(PLind&PLleft), '.r');
plot(find(RQind&RQleft), RQy(RQind&RQleft), '<g');

plot(find(PHind&PHleft), PHy(PHind&PHleft), 'xb');
plot(find(PLind&PLright),PLy(PLind&PLright),'xr');
plot(find(RQind&RQright),RQy(RQind&RQright),'>g');

% stairs(1:length(hits),cumsum(hits|PLright)'./(1:length(hits))*100,'-k')
% % stairs(1:length(PHind),cumsum(PHright|Qhits)./cumsum(PHind|PLind)*100,'-k')

plot([0 length(trials)],[50 50],'-k');

xlim([0 length(trials)]);
ylim([-10 110]);
set(gca,'ytick',[0 25 50 75 100]);
box on
xlabel('Trial number');
ylabel('Hit rate');
hold off
legend(sprintf('Probe Higher (%0.1f%%)',PHhr), ...
    sprintf('Probe Lower (%0.1f%%)',PLhr), ...
    sprintf('Quiet (lft bias) (%0.1f%%)',RQlb), ...
    'Overall','location','best')


% %% dprime
% 
% SignalTrials = bitget(vals,11) | bitget(vals,13);
% nSignalTrials = sum(SignalTrials);
% HR = sum(PHright | RQleft)/nSignalTrials;
% 
% CatchTrials = bitget(vals,12);
% nCatchTrials = sum(CatchTrials);
% FA = sum(PLright)/nCatchTrials;
% 
% [d,beta,c] = dprime(HR,FA,nSignalTrials,nCatchTrials);
% 
% fprintf('d'' = %0.3f\tbeta = %0.3f\tc = %0.3f\n',d,beta,c)






